package org.jeecg.modules.basic.iouser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;
import org.jeecg.modules.basic.guest.service.ILwgyInoutGuestService;
import org.jeecg.modules.basic.iouser.entity.LwgyInoutUser;
import org.jeecg.modules.basic.iouser.service.ILwgyInoutUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Description: 出入人员名单
* @Author: jeecg-boot
* @Date:   2021-03-25
* @Version: V1.0
*/
@Api(tags="出入人员名单")
@RestController
@RequestMapping("/iouser/lwgyInoutUser")
@Slf4j
public class LwgyInoutUserController extends JeecgController<LwgyInoutUser, ILwgyInoutUserService> {
   @Autowired
   private ILwgyInoutUserService lwgyInoutUserService;
   @Autowired
   private ILwgyInoutGuestService lwgyInoutGuestService;
   @Autowired
   private ILwgyInoutRecordService lwgyInoutRecordService;

   /**
    * 分页列表查询
    *
    * @param lwgyInoutUser
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "出入人员名单-分页列表查询")
   @ApiOperation(value="出入人员名单-分页列表查询", notes="出入人员名单-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(LwgyInoutUser lwgyInoutUser,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<LwgyInoutUser> queryWrapper = QueryGenerator.initQueryWrapper(lwgyInoutUser, req.getParameterMap());
       Page<LwgyInoutUser> page = new Page<LwgyInoutUser>(pageNo, pageSize);
       IPage<LwgyInoutUser> pageList = lwgyInoutUserService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param lwgyInoutUser
    * @return
    */
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody LwgyInoutUser lwgyInoutUser) {
       lwgyInoutUserService.save(lwgyInoutUser);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param lwgyInoutUser
    * @return
    */
   @AutoLog(value = "出入人员名单-编辑")
   @ApiOperation(value="出入人员名单-编辑", notes="出入人员名单-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody LwgyInoutUser lwgyInoutUser) {
       lwgyInoutUserService.updateById(lwgyInoutUser);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "出入人员名单-通过id删除")
   @ApiOperation(value="出入人员名单-通过id删除", notes="出入人员名单-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       lwgyInoutUserService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "出入人员名单-批量删除")
   @ApiOperation(value="出入人员名单-批量删除", notes="出入人员名单-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.lwgyInoutUserService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "出入人员名单-通过id查询")
   @ApiOperation(value="出入人员名单-通过id查询", notes="出入人员名单-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       LwgyInoutUser lwgyInoutUser = lwgyInoutUserService.getById(id);
       if(lwgyInoutUser==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(lwgyInoutUser);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param lwgyInoutUser
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, LwgyInoutUser lwgyInoutUser) {
       return super.exportXls(request, lwgyInoutUser, LwgyInoutUser.class, "出入人员名单");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, LwgyInoutUser.class);
   }

    @AutoLog(value = "出入人员名单-人员信息采集")
    @ApiOperation(value="出入人员名单-人员信息采集", notes="出入人员名单-人员信息采集")
    @PostMapping(value = "/iouserCollect")
    public Result<?> iouserCollect(@RequestBody LwgyInoutUser lwgyInoutUser) throws Exception {
       try{
            lwgyInoutUserService.save(lwgyInoutUser);
            return Result.OK("添加成功！");
        }catch (Exception e){
            return Result.error("添加失败！");
        }
    }
   //学生修改绑定的人员组
   @PostMapping(value = "/iouserChange")
    public Boolean iouserChange(@RequestParam(name="studentId",required=true) String studentId,
                                @RequestParam(name="buildingId",required=true) String buildingId) throws Exception {
        // 1.通过studentid获取lwgyInoutUser
        LwgyInoutUser lwgyInoutUser = lwgyInoutUserService.getUserByStudentId(studentId);// = lwgyInoutUserService;
       if(lwgyInoutUser ==null || lwgyInoutUser.getGuestId()==null || lwgyInoutUser.getGuestId().equals("null")){
               return false;
           }
        System.out.println("lwgyInoutUser=====:"+lwgyInoutUser);
        //2.根据调整后的楼id获取人员组（人员组表中要添加关联楼宇）
        String group = lwgyInoutUserService.getGroupIdByBuildingId(buildingId);
        System.out.println("group=====:"+group);
        lwgyInoutUser.setGroups(group);
        //3.调取接口
        String result = lwgyInoutUserService.updateIouserToPublic(lwgyInoutUser);
        JSONObject json = JSONObject.fromObject(result);
        String code = json.getString("code");
        String message = json.getString("message");
        if("200".equals(code)){
            return true;
        }else{
            return false;
        }
    }

    /**
     *   获取人员二维码,，通过学号查询studentId查询userId，后与访客同一接口
     *
     * @return
     */
    @AutoLog(value = "获取人员二维码")
    @ApiOperation(value="获取人员二维码", notes="获取人员二维码")
    @PostMapping(value = "/userQrCode")
    public Result<?> userQrCode(@RequestParam(name="studentId",required=true) String studentId) throws Exception {

        LwgyBaseStudent LwgyBaseStudent = lwgyInoutRecordService.queryStuByNumString(studentId);
        LwgyInoutUser lwgyInoutUser = lwgyInoutUserService.getUserByStudentId(LwgyBaseStudent.getId());// = lwgyInoutUserService;
        String userId="";
        if(lwgyInoutUser.getGuestId()==null || lwgyInoutUser.getGuestId().equals("null")){
            return Result.error("没有获取到第三方id");
        }else {
            userId=lwgyInoutUser.getGuestId();
        }

        String groupString = lwgyInoutGuestService.queryQrcodeByIdToPrivate(userId);
        return Result.OK(groupString);
    }

    /**
     *   人像图片质量检测(公有云)
     *
     * @param avatarFile
     * @return
     */
    @AutoLog(value = "出入人员名单-人像图片质量检测(公有云)")
    @ApiOperation(value="出入人员名单-人像图片质量检测(公有云)", notes="出入人员名单-人像图片质量检测(公有云)")
    @PostMapping(value = "/qualityToPublic")
    public Result<?> qualityToPublic(@RequestParam(name="avatarFile") String avatarFile) throws Exception {
        //添加商汤系统中
        System.out.println("avatarFile"+avatarFile);
        String result = lwgyInoutUserService.qualityToPublic(avatarFile);
        JSONObject json = JSONObject.fromObject(result);
        String code = json.getString("code");
        String message = json.getString("message");
        if("200".equals(code)){
            System.out.println("result"+result);
            JSONObject data = json.getJSONObject("data");
            return Result.OK("添加成功！");
        }else{
            return Result.error(code);
        }

    }
    /**
     *   人像图片质量检测(私有云)
     *
     * @param avatarFile
     * @return
     */
    @AutoLog(value = "出入人员名单-人像图片质量检测(私有云)")
    @ApiOperation(value="出入人员名单-人像图片质量检测(私有云)", notes="出入人员名单-人像图片质量检测(私有云)")
    @PostMapping(value = "/qualityToPrivate")
    public Result<?> qualityToPrivate(@RequestParam(name="avatarFile") String avatarFile) throws Exception {
        //添加商汤系统中
        System.out.println("avatarFile"+avatarFile);
        String result = lwgyInoutUserService.qualityToPrivate(avatarFile);
        JSONObject json = JSONObject.fromObject(result);
        return Result.OK(json);

    }

    /**
     * 查询所有分组
     */
    @AutoLog(value = "查询所有分组")
    @ApiOperation(value = "查询所有分组", notes = "查询所有分组")
    @GetMapping(value = "/getGroupData")
    public Result<?> getGroupData(@RequestParam(name = "type") String type) {

        List<Map<String, String>> maps = lwgyInoutUserService.getGroupData(type);
        maps.forEach(System.out::println);
        return Result.OK(maps);
    }


    /**
     *  从第三方获取人员组关系
     *
     *
     * @return
     */
    @AutoLog(value = "出入人员名单- 从第三方获取人员组关系")
    @ApiOperation(value="出入人员名单- 从第三方获取人员组关系", notes="出入人员名单- 从第三方获取人员组关系")
    @PostMapping(value = "/getGroupToOther")
    public Result<?> getGroupToOther(){

        return Result.OK();

    }

    /**
     *  更新人员组关系
     *
     *
     * @return
     */
    @AutoLog(value = "出入人员名单-更新人员组关系")
    @ApiOperation(value="出入人员名单-更新人员组关系", notes="出入人员名单-更新人员组关系")
    @PostMapping(value = "/changeGroupToOther")
    public Result<?> changeGroupToOther(){
        //lwgyInoutUserService.get
        return Result.OK();

    }
}
